package com.atguigu.tingshu.common.config;


import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

@Configuration
@Slf4j
public class RabbitConfig {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void  init(){

        //确认消息是否到达交换机
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause)->{
            if (!ack){
                //如果没有到达
            log.error("消息没能到达交换机"+cause);
            }

        });
        //确认消息是否到达队列
        this.rabbitTemplate.setReturnsCallback(returnedMessage -> {

        log.error("消息没有到达队列、交换机：消息内容 路由键 回调状态"+returnedMessage.getExchange()
                +returnedMessage.getRoutingKey()+new String(returnedMessage.getMessage().getBody())
                +returnedMessage.getReplyText()
               );
        });
    }





}
